package MyCustomTable;

import MyUtil.*;
import redis.clients.jedis.Jedis;

import javax.swing.*;
import java.awt.*;
import java.sql.*;

public class Manager_Author_Set extends JFrame{
    //sql语句
    private String sql ="select Eaccount 员工账号,Ename 员工姓名,Elevel 员工等级 from Employee";
    private String sql2 = "Update Employee set ELevel = ? where EACCOUNT = ?";

    private JScrollPane jScrollPane;
    private JButton change_button = new JButton("修改(根据)");
    private JLabel tip_label = new JLabel("提示:销售员为1,管理员为2");
    private JLabel eid_label = new JLabel("想要修改的员工账号：");
    private JLabel elevel_label = new JLabel("修改成的等级:");
    private JTextField eid_TextField = new JTextField(10);
    private JTextField elevel_TextField = new JTextField(5);
    private JPanel myPanel;
    private JPanel myPanel2;
    public Manager_Author_Set() throws SQLException {
        super("员工权限修改");
        //基础设置
        this.setBounds(400,200,1000,600);
        //设置控件
        myPanel = new MyPanel();
        this.setContentPane(myPanel);
        myPanel2 = new MyPanel("/picture/5.jpg");
        //设置布局
        myPanel.setLayout(new BorderLayout());
        myPanel2.setLayout(new FlowLayout());
        //ScrollPane的设置
        jScrollPane = JScrollUtil.getCustomJScrollPane(DataBaseUtil.produceResultSet(sql));
        //加入控件
        myPanel.add(jScrollPane,BorderLayout.WEST);
        myPanel.add(tip_label,BorderLayout.PAGE_START);
        myPanel.add(myPanel2,BorderLayout.EAST);

        myPanel2.add(eid_label);
        myPanel2.add(eid_TextField);
        myPanel2.add(elevel_label);
        myPanel2.add(elevel_TextField);
        myPanel2.add(change_button);
      //button设置
        change_button.addActionListener((e)->{
            String p1 = eid_TextField.getText();
            String p2 = elevel_TextField.getText();
            String[] p3 =  new String[2];
            p3[0] = p2;
            p3[1] = p1;
            if((p1.equals("")) ||(p2.equals(""))){
                JOptionPane.showMessageDialog(this,"有输入值为空","错误",JOptionPane.PLAIN_MESSAGE);
            }else if((p2.equals("1")) ||(p2.equals("2"))){
                try {
                    Jedis jedis = RedisUtil.getConRedis();
                    jedis.lset(p1,1,p2);
                    DataBaseUtil.changeResultSet(sql2,p3);
                    myPanel.remove(jScrollPane);
                    jScrollPane = JScrollUtil.getCustomJScrollPane(DataBaseUtil.produceResultSet(sql));
                    myPanel.add(jScrollPane,BorderLayout.WEST);
                    this.setVisible(false);
                    this.setVisible(true);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            else{
                JOptionPane.showMessageDialog(this,"有输入值不规范","错误",JOptionPane.PLAIN_MESSAGE);
            }
        });

        this.setVisible(true);

    }
}
